package com.wesley.application;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE;

/**
 * @author:wesley
 */
public class MyZuulFilter extends ZuulFilter {


    @Override
    public String filterType() {

        // 四种类型
        /*String ERROR_TYPE = "error";
        String POST_TYPE = "post";
        String PRE_TYPE = "pre";
        String ROUTE_TYPE = "route";*/

        return PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {
        RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletRequest request = requestContext.getRequest();
        Enumeration<String> headerNames = requestContext.getRequest().getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();

            System.out.println("------------HEADER:"+name+"-------------");

            Enumeration<String> values = request.getHeaders(name);

            while (values.hasMoreElements()) {
                String value = values.nextElement();
                System.out.println(value);
            }
            System.out.println("------------HEADER:"+name+"------------");
        }
        return null;
    }
}
